export default {

	data() {
		return {
			imgUrl: this.baseApi + '/file/getImgStream?idFile=',
			uploadAction: this.baseApi +'/file/upload',
			header: {
				Authorization: '',
				RAuthorization:'',
			},
			border: false,
			activeFooter: 3,
			title: '',
			orderItemTemp: {
				orderSn: '',
				itemid: ''
			},
			orderItemTemp2: {
				id: '',
				goods: {
					name: '',
					descript: '',
					count: '',
					pic: '',
				},

			},
			expressInfo: {},
			order: {},
			refundData: {

				afterSalesMode: '',
				refundReason: '',
				refundAmt: 0.0,
				mobile: 0,
				images: []

			},

			rules: {
				afterSalesMode: [{
					required: true,
					message: '请选择售后方式',
					trigger: 'change'
				}],
				refundReason: [{
					required: true,
					message: '请选择退款原因',
					trigger: 'change'
				}],
				refundAmt: [{
						required: true,
						message: '请输入退款金额'
					},
					{
						// 此为同步验证，可以直接返回true或者false，如果是异步验证，稍微不同，见下方说明
						validator: (rule, value, callback) => {
							// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
							return value <= this.formatPrice(this.orderItemTemp2.price * this.orderItemTemp2
								.count);
						},
						message: '输入的金额必须小于等于最多可退金额',
						// 触发器可以同时用blur和change，二者之间用英文逗号隔开
						trigger: ['change', 'blur'],
					}
				],
				mobile: [{
						required: true,
						message: '请输入手机号',
						trigger: ['change', 'blur'],
					},
					{
						validator: (rule, value, callback) => {
							// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
							return this.$u.test.mobile(value);
						},
						message: '手机号码不正确',
						// 触发器可以同时用blur和change，二者之间用英文逗号隔开
						trigger: ['change', 'blur'],
					}
				],
				refundMsg:[
					{
						max: 100,
						message: '最多输入100个字',
						trigger: ['change','blur'],
					},
				],
			},

			// imgUrl: this.baseApi + '/file/getImgStream?idFile=',
			afterSalesModeInfo: {
				show: false,
				selectInfo: {
					text: '',
					val: ''
				},
				actions: [{
					text: '仅退款',
					val: '1'
				}, {
					text: '退款退货',
					val: '2'
				}],
			},
			refundReason: {
				show: false,
				selectInfo: {
					text: '',
					val: ''
				},
				//①、退运费；②、收到商品破损 ③、商品错发、漏发 ；④、商品需要维修 ；⑤、发票dao问题；⑥、收到商品与描述不符；⑦、商品质量问题；⑧、未按约定时间发货。⑨、其他
				actions: [{
						text: '退运费',
						val: '1'
					},
					{
						text: '收到商品破损',
						val: '2'
					},
					{
						text: '商品错发、漏发',
						val: '3'
					},
					{
						text: '商品需要维修',
						val: '4'
					},
					{
						text: '发票问题',
						val: '5'
					},
					{
						text: '收到商品与描述不符',
						val: '6'
					},
					{
						text: '商品质量问题',
						val: '7'
					},
					{
						text: '未按约定时间发货',
						val: '8'
					},
					{
						text: '不想要了',
						val: '9'
					},
					{
						text: '其他',
						val: '10'
					}
				],
			},
			fileList: [],
			errorType: ['message'],

		};
	},
	onLoad(option) {

		this.orderItemTemp.orderSn = option.orderSn;
		this.orderItemTemp.itemid = option.itemid;
		this.uploadAction= this.baseApi +'/file/upload';
		this.header.Authorization = this.vuex_token;
		this.header.RAuthorization = this.vuex_rtoken; 
		this.init();
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	mounted() {

	},
	methods: {
		init() {

			this.getData()

		},
		getData() {

			this.$u.get('user/order/' + this.orderItemTemp.orderSn).then(res => {
				this.order = res;

				for (let index in this.order.items) {
					let item = this.order.items[index];
					if (item.id == this.orderItemTemp.itemid) {
						this.orderItemTemp2 = item;
					}
				}
				this.refundData.orderSn = this.orderItemTemp.orderSn;
				this.refundData.itemid = this.orderItemTemp.itemid;
				this.refundData.refundAmt = this.formatPrice(this.orderItemTemp2.price * this.orderItemTemp2
					.count);
				this.refundData.mobile = this.order.mobile;
				this.refundData.refundMsg = "";

			})

		},
		formatPrice(price) {
			return (price / 100).toFixed(2);
		},
		onClickLeft() {
			this.$router.go(-1)
		},
		onAfterSalesModeSelect(index) {
			// 默认情况下点击选项时不会自动收起
			// 可以通过 close-on-click-action 属性开启自动收起
			uni.hideKeyboard();
			let item = this.afterSalesModeInfo.actions[index];
			this.afterSalesModeInfo.show = false;
			//Toast(item.name + item.val);
			this.afterSalesModeInfo.selectInfo = item;
			this.refundData.afterSalesMode = item.val;
			this.selectInfo1 = item.text;
		},
		onRefundReasonSelect(index) {
			// 默认情况下点击选项时不会自动收起
			// 可以通过 close-on-click-action 属性开启自动收起
			uni.hideKeyboard();
			let item = this.refundReason.actions[index];
			this.refundReason.show = false;
			//Toast(item.name + item.val);
			this.refundReason.selectInfo = item;
			this.refundData.refundReason = item.val;
		},
		afterRead(vantFile) {
			userApi.upload({
				name: vantFile.file.name,
				type: vantFile.file.type,
				base64: vantFile.content
			}).then(response => {
				// let iurl = baseApi + '/file/getImgStream?idFile=' + response.data.avatar;
				this.refundData.images.push(response.data.realFileName);
			})
		},

		uploadSuccess(response) {
			this.refundData.images.push(response.data.realFileName);
		},
		uploadError() {
			this.$u.toast('上传失败');
		},

		uploadRemove(index, lists, name) {
			this.refundData.images.splice(index, 1);
		},
		refundApplyValidate() {
			this.$refs.uForm.validate(valid => {
				if (valid) {
					console.log('验证通过');
					this.refundApply();
				} else {
					console.log('验证失败');
				}
			});
		},
		refundApply() {
			let reqdata = {};
			let msgs = {};
			for (var key in this.refundData) {

				reqdata[key] = this.refundData[key];

				if ("orderSn" === key || "itemid" === key) {
					continue;
				}

				if ("images" === key) {
					msgs[key] = this.refundData[key].join();
				} else {
					msgs[key] = this.refundData[key];
				}


			}

			reqdata.mapMsg = msgs;
			
			this.$u.post('M0001', reqdata).then(response => {

				this.$u.route({
					url: '/pages1/order/detail',
					params: {
						orderSn: this.orderItemTemp.orderSn,
						orderid: this.order.id
					}
				})
				
				
		
			}).catch(err => {
				console.log('err', err)
				this.$u.toast(err);
			})
		}
	}
}
